﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BUS;
namespace ManagementBooks
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }
        NhanVien_BUS _nhanvienBUS = new NhanVien_BUS();
        public delegate void SendMessage(string TenTK);
        public SendMessage Sender;
        public static string tennhanvien = "";
        public static string tenTaiKhoana = "";
        private void Main_Load(object sender, EventArgs e)
        {
            Sender = new SendMessage(GetMessage);
        }
        private void GetMessage(string TenTK)
        {
            TenTaiKhoan = TenTK;
            tenTaiKhoana = TenTK;
        }
        string TenTaiKhoan;
        Login dn = null;
        Login dn1 = new Login();
         public Main(string tendangnhap):this()
        {
            TenTaiKhoan = tendangnhap;
            DataTable dt = new DataTable();
            dt = _nhanvienBUS.Thong_Tin_Nhan_Vien(tendangnhap);
            lbHoVaTen.Text = dt.Rows[0]["HoVaTen"].ToString();
            tennhanvien = dt.Rows[0]["HoVaTen"].ToString(); 
            lbNhomQuyen.Text = dt.Rows[0]["NhomQuyen"].ToString();
            lbChucVu.Text = dt.Rows[0]["ChucVu"].ToString();
            int check = (_nhanvienBUS.PhanQuyen_NguoiDung(tendangnhap));
            switch (check)
            {
                case 1:
                    Enable();
                    break;//TH 1 Admin  
                case 2:
                    break;//TH2 Nhan Vien
                case 3:
                    break;//TH3 Guest
            }
        }
         public void TabCreating(TabControl TabControl, string Text, Form Form)
         {
             int Index = KiemTraTonTai(TabControl, Text);
             if (Index >= 0)
             {
                 TabControl.SelectedTab = TabControl.TabPages[Index];
                 TabControl.SelectedTab.Text = Text;
             }
             else
             {
                 TabPage TabPage = new TabPage { Text = Text };
                 TabControl.TabPages.Add(TabPage);
                 TabControl.SelectedTab = TabPage;
                 Form.TopLevel = false;
                 Form.Parent = TabPage;
                 //  Form.MdiParent = this;
                 Form.Show();
                 Form.Dock = DockStyle.Fill;
             }
         }
         static int KiemTraTonTai(TabControl TabControlName, string TabName)
         {
             int temp = -1;
             for (int i = 0; i < TabControlName.TabPages.Count; i++)
             {
                 if (TabControlName.TabPages[i].Text == TabName)
                 {
                     temp = i;
                     break;
                 }
             }
             return temp;
         }
         private void timer1_Tick_1(object sender, EventArgs e)
         {
             label1.Text = label1.Text.Substring(1, label1.Text.Length - 1) + label1.Text.Substring(0, 1);
         }
         public void Enable()
         {
             mnuDanhMuc.Enabled = true;
             mnuQuanLy.Enabled = true;
             mnuNV.Enabled = true;
             tsbKhachHang.Enabled = true;
             tsbNhaCC.Enabled = true;
             tsbPNS.Enabled = true;
             tstTKBC.Enabled = true;
             tsbThongTinSach.Enabled = true;
             mnuDN.Enabled = false;
             mnuDX.Enabled = true;
             btPhieuXuat.Enabled = true;
         }
         public void Disable()
         {
             mnuDanhMuc.Enabled = false;
             mnuQuanLy.Enabled = false;
             mnuNV.Enabled = false;
             tsbKhachHang.Enabled = false;
             tsbNhaCC.Enabled = false;
             tsbPNS.Enabled = false;
             tstTKBC.Enabled = false;
             tsbThongTinSach.Enabled = false;
             mnuDN.Enabled = true;
             mnuDX.Enabled = false;
         }

         private void mnuDN_Click(object sender, EventArgs e)
         {
             if (dn == null || dn.IsDisposed)
                 dn = new Login();
             dn.ShowDialog();
             this.Close();
             if (dn.temp == true)
             {
                 Enable();
             }
         }
         private void dongHetCuaSo()
         {
             while (this.ActiveMdiChild != null)
                 this.ActiveMdiChild.Dispose();
         }
         private void dongHetCuaSoTruHienHanh()
         {
             int n = this.MdiChildren.Length - 1;
             Form[] f = this.MdiChildren;
             Form fcurent = this.ActiveMdiChild;
             while (n != -1)
             {
                 if (f[n] != fcurent)
                 {
                     f[n].Dispose();
                 }
                 n--;
             }
         }

         private void toolStripMenuItem1_Click(object sender, EventArgs e)
         {
             this.dongHetCuaSo();
         }

         private void toolStripMenuItem2_Click(object sender, EventArgs e)
         {
             this.dongHetCuaSoTruHienHanh();
         }

         private void mnuDX_Click(object sender, EventArgs e)
         {
             this.Disable();
             this.dongHetCuaSo();
             dn1.temp = false;
         }

         private void mnuThoat_Click(object sender, EventArgs e)
         {
             if (DialogResult.Yes == MessageBox.Show("Bạn có chắc muốn thoát hay không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
             {
                 Application.Exit();
             }
         }

         private void mnuNhanVien_Click(object sender, EventArgs e)
         {
             NhanVien nhanvien = new NhanVien();
             TabCreating(tabControl1, "Nhân viên", nhanvien);
         }

         private void mnuKhachHang_Click(object sender, EventArgs e)
         {
             KhachHang khachhang = new KhachHang();
             TabCreating(tabControl1, "Khách hàng", khachhang);
         }

         private void tabControl1_MouseDown_1(object sender, MouseEventArgs e)
         {
             for (int i = 1; i < this.tabControl1.TabPages.Count; i++)
             {
                 Rectangle rPage = tabControl1.GetTabRect(i);
                 Rectangle closeButton = new Rectangle(rPage.Right - 15, rPage.Top + 5, 10, 10);
                 if (closeButton.Contains(e.Location))
                 {
                     if (MessageBox.Show("Bạn Có Muốn Tắt Tab Này?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                     {
                         this.tabControl1.TabPages.RemoveAt(i);
                         break;
                     }
                 }
             }
         }

         private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
         {
             Font closefont = new Font(e.Font.FontFamily, e.Font.Size, FontStyle.Bold);
             Font titlefont = new Font(e.Font.FontFamily, e.Font.Size, FontStyle.Italic);
             if (e.Index > 0)
             {
                 e.Graphics.DrawString("X", closefont, Brushes.Blue, e.Bounds.Right - 15, e.Bounds.Top + 5);
             }
             e.Graphics.DrawString(this.tabControl1.TabPages[e.Index].Text, titlefont, Brushes.Black, e.Bounds.Left, e.Bounds.Top + 5);
         }

         private void tsbThongTinSach_Click(object sender, EventArgs e)
         {
             DanhMucSach danhmucsach = new DanhMucSach();
             danhmucsach.Sender(TenTaiKhoan);
             danhmucsach.Show();
         }

         private void mnuTTSach_Click(object sender, EventArgs e)
         {
             DanhMucSach danhmucsach = new DanhMucSach();
             danhmucsach.Sender(TenTaiKhoan);
             danhmucsach.Show();
         }

         private void tsbNhaCC_Click(object sender, EventArgs e)
         {
             NXB nhaxb = new NXB();
             nhaxb.Show();
         }

         private void mnuDMLS_Click(object sender, EventArgs e)
         {
             TheLoai theloai = new TheLoai();
             theloai.Show();
         }

         private void mnuDMS_Click(object sender, EventArgs e)
         {
             DanhMucSach danhmucsach = new DanhMucSach();
             danhmucsach.Show();
         }

         private void tsbKhachHang_Click(object sender, EventArgs e)
         {
             KhachHang khachhang = new KhachHang();
             TabCreating(tabControl1, "Khách hàng", khachhang);
         }
         #region=====Xử lý ảnh nền==========
         /* Các việc phải làm
         * 1: lấy ảnh từ đường dẫn
         * 2:lọc ảnh
         * 3:Hiển thị :D
         */
         OpenFileDialog ofd = new OpenFileDialog(); 
         private void tsbPNS_Click(object sender, EventArgs e)
         {
             ofd.Title = "Mở thư mục chứa ảnh ";
             ofd.InitialDirectory = @"C:\";
             ofd.Filter = "Image|*.jpg;*.png;*.jpeg;*.gif;*.bmp";
             if (ofd.ShowDialog() == DialogResult.OK)
             {
                 pictureBox1.Image = Image.FromFile(ofd.FileName);

             }
         }
         #endregion
         private void mnuPhieuXuatSach_Click(object sender, EventArgs e)
         {
             XuatSach xuatsach = new XuatSach();
             xuatsach.Sender(TenTaiKhoan);
             TabCreating(tabControl1, "Phiếu xuất sách", xuatsach);
         }

         private void toolStripButton1_Click(object sender, EventArgs e)
         {
             XuatSach xuatsach = new XuatSach();
             xuatsach.Sender(TenTaiKhoan);
             TabCreating(tabControl1, "Phiếu xuất sách", xuatsach);
         }

         private void backupAndRestoreToolStripMenuItem_Click(object sender, EventArgs e)
         {
             BackupAndRestore backRes = new BackupAndRestore();
             backRes.Show();
         }
         private void tstTKBC_Click(object sender, EventArgs e)
         {
             ThongKe thongke = new ThongKe();
             TabCreating(tabControl1, "Thống kê báo cáo",thongke);
         }

         private void thốngKêBáoCáoToolStripMenuItem_Click(object sender, EventArgs e)
         {
             ThongKe thongke = new ThongKe();
             TabCreating(tabControl1, "Thống kê báo cáo", thongke);
         }

         private void helpToolStripMenuItem1_Click(object sender, EventArgs e)
         {
             Help.ShowHelp(this, "HelpQLNS.chm");
         }
    }
}
